package com.aat.dxfy.autoCode;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class BaseUIMongoCode {
	private String baseDir;// xlsm所在基本目录
	private String xlsmName;// 文件名
	private String packagePath;// com.公司名.项目名

	private String moduleName;// 模块名
	private String packageName;// com.公司名.项目名
	private String beanName;
	private String tableName;
	private List<TabField> fields = new ArrayList<>();
	private String javaMakeCodeDir = "javaMongoCodeDir";

	public static void main(String[] args) {
		BaseUIMongoCode b = new BaseUIMongoCode("D:\\", "model.xlsm", "com/aat/dizena");
		try {
			b.getBaseInfo4Xlsm();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public BaseUIMongoCode(String baseDir, String xlsmName, String packagePath) {
		this.baseDir = baseDir;
		this.xlsmName = xlsmName;
		this.packagePath = packagePath;
		packageName = packagePath.replaceAll("/", ".");// com.公司名.项目名
	}

	// 0,读取xlsm 形式
	public void getBaseInfo4Xlsm() throws Exception {
		XSSFWorkbook xssfWorkbook = new XSSFWorkbook(baseDir + xlsmName);
		// 读取目录
		XSSFSheet xssfSheetML = xssfWorkbook.getSheetAt(0);
		for (int i = 1; i <= xssfSheetML.getLastRowNum(); i++) {
			XSSFRow rowML = xssfSheetML.getRow(i);
			if (rowML == null) {
				continue;
			}

			String tbName = null;
			String isOk = null;
			try {
				tbName = getXlssVal(rowML.getCell(2));
				isOk = getXlssVal(rowML.getCell(4));
			} catch (NullPointerException q) {
				continue;
			}

			if (isNotEmpty(tbName) && isNotEmpty(isOk)) {
				if ("yes".equals(isOk)) {
					XSSFSheet xssfSheet = xssfWorkbook.getSheet(tbName);
					//
					if (null != xssfSheet) {
						// 装数
						tableName = xssfSheet.getSheetName();
						if (tableName.contains("t_")) {
							tableName = tableName.replace("t_", "");
						}
						String[] ss = tableName.split("_");
						moduleName = ss[0];
						String str = ss[1];
						beanName = str.substring(0, 1).toUpperCase() + str.substring(1, str.length());
						fields.clear();
						//
						for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
							XSSFRow xssfRow = xssfSheet.getRow(rowNum);
							String desc = getXlssVal(xssfRow.getCell(1));
							String name = getXlssVal(xssfRow.getCell(2));
							String type = getXlssVal(xssfRow.getCell(3));
							String flag = getXlssVal(xssfRow.getCell(4));

							TabField bean = new TabField(desc, name, type, flag);
							fields.add(bean);
						}
						// 代码
						createJavaBean();

						createDao1();

						createDao2();

						createService();

						createServiceImpl();

						// end
					}
				}
			}
		}
		xssfWorkbook.close();
		//
	}

	// 2,根据读取的信息，创建实体类,{entity}.java
	public void createJavaBean() {
		// 创建包
		String javaFilePath = baseDir + javaMakeCodeDir + File.separator + packagePath + File.separator + moduleName
				+ File.separator + "bean";
		createPackage(javaFilePath);
		// 代码
		StringBuffer sb = new StringBuffer();
		sb.append("package " + packageName + "." + moduleName + ".bean;\n\n");// 包
		sb.append("import java.io.Serializable;\n");
		sb.append("import java.util.*;\n\n");

		sb.append("import org.mongodb.morphia.annotations.Entity;\n");
		sb.append("import org.mongodb.morphia.annotations.Id;\n");
		sb.append("import org.mongodb.morphia.annotations.Indexed;\n");
		sb.append("import org.mongodb.morphia.annotations.Indexes;\n\n");

		sb.append("@Entity(value = \"t_" + tableName + "\", noClassnameStored = true)\n");
		sb.append("@Indexes({})\n");
		sb.append("public class " + beanName + "  implements Serializable{\n");// 类

		sb.append("    private static final long serialVersionUID = 1L;\n");
		for (TabField tabField : fields) {
			if ("id".equalsIgnoreCase(tabField.getFlag())) {
				sb.append("    @Id\n");
			}
			if ("index".equalsIgnoreCase(tabField.getFlag())) {
				sb.append("    @Indexed\n");
			}
			sb.append("    private " + tabField.getType() + " " + tabField.getName() + ";\n");
		}

		// set get
		sb.append("\n");
		for (TabField tabField : fields) {
			String fn1 = tabField.getName();
			String fn2 = fn1.substring(0, 1).toUpperCase() + fn1.substring(1, fn1.length());
			sb.append("    public " + tabField.getType() + " get" + fn2 + "() {\n");
			sb.append("        return " + fn1 + ";\n");
			sb.append("    }\n\n");

			sb.append("    public void set" + fn2 + "(" + tabField.getType() + " " + fn1 + ") {\n");
			sb.append("        this." + fn1 + " = " + fn1 + ";\n");
			sb.append("    }\n\n");
		}

		// hashcode
		sb.append("    public int hashCode() {\n");
		sb.append("        final int prime = 31;\n");
		sb.append("        int result = 1;\n");
		sb.append("        result = prime * result + ((id == null) ? 0 : id.hashCode());\n");
		sb.append("        return result;\n");
		sb.append("    }\n\n");

		// equals
		sb.append("    public boolean equals(Object obj) {\n");
		sb.append("        if (this == obj)\n");
		sb.append("            return true;\n");
		sb.append("        if (obj == null)\n");
		sb.append("            return false;\n");
		sb.append("        if (getClass() != obj.getClass())\n");
		sb.append("            return false;\n");
		sb.append("        " + beanName + " other = (" + beanName + ") obj;\n");
		sb.append("        if (id == null) {\n");
		sb.append("            if (other.id != null)\n");
		sb.append("                return false;\n");
		sb.append("        } else if (!id.equals(other.id))\n");
		sb.append("            return false;\n");
		sb.append("        return true;\n");
		sb.append("    }\n\n");

		// tostr
		sb.append("    public String toString() {\n");
		sb.append("       return \"" + beanName + " [id=\"+ id +\"]\";\n");
		sb.append("    }\n\n");

		// ok
		sb.append("}\n");
		writeS(new String(sb), "utf-8", javaFilePath + File.separator + beanName + ".java");
	}

	// 3-1,根据读取的信息，创建实体Dao,{entity}Dao.java
	public void createDao1() {
		// 创建包
		String javaFilePath = baseDir + javaMakeCodeDir + File.separator + packagePath + File.separator + moduleName
				+ File.separator + "dao" + File.separator + "mongo";
		createPackage(javaFilePath);
		// 代码
		StringBuffer sb = new StringBuffer();
		sb.append("package " + packageName + "." + moduleName + ".dao.mongo;\n\n");// 包

		sb.append("import org.bson.types.ObjectId;\n");
		sb.append("import org.mongodb.morphia.Datastore;\n");
		sb.append("import org.mongodb.morphia.query.Query;\n");
		sb.append("import org.mongodb.morphia.dao.BasicDAO;\n");
		sb.append("import org.springframework.beans.factory.annotation.Autowired;\n");
		sb.append("import org.springframework.beans.factory.annotation.Qualifier;\n");
		sb.append("import org.springframework.stereotype.Repository;\n\n");
		sb.append("import " + packageName + "." + moduleName + ".bean." + beanName + ";\n\n");// 导包

		sb.append("@Repository\n");
		sb.append("public class " + beanName + "Dao extends BasicDAO<" + beanName + ", ObjectId>{\n\n");// 类

		sb.append("    @Autowired\n");
		sb.append("    public " + beanName + "Dao(@Qualifier(\"base-datastore\") Datastore ds) {\n");
		sb.append("        super(ds);\n");
		sb.append("    }\n\n");

		sb.append("    public Query<" + beanName + "> getQuery() {\n");
		sb.append("        return this.createQuery();\n");
		sb.append("    }\n\n");

		sb.append("}\n");
		writeS(new String(sb), "utf-8", javaFilePath + File.separator + beanName + "Dao.java");
	}

	// 3-2,根据读取的信息，创建实体Dao,{entity}Dao.java
	public void createDao2() {
		// 创建包
		String javaFilePath = baseDir + javaMakeCodeDir + File.separator + packagePath + File.separator + moduleName
				+ File.separator + "dao" + File.separator + "redis";
		createPackage(javaFilePath);
		// 代码
		StringBuffer sb = new StringBuffer();
		sb.append("package " + packageName + "." + moduleName + ".dao.redis;\n\n");// 包

		sb.append("import org.springframework.beans.factory.annotation.Autowired;\n");
		sb.append("import org.springframework.stereotype.Repository;\n");
		sb.append("import org.springframework.data.redis.core.RedisTemplate;\n");
		sb.append("import com.aat.dizena.base.dao.redis.BaseRedisTemplate;\n\n");

		sb.append("import " + packageName + "." + moduleName + ".bean." + beanName + ";\n\n");// 导包

		sb.append("@Repository\n");
		sb.append("public class " + beanName + "RedisTemplate extends BaseRedisTemplate<" + beanName + ">{\n\n");// 类

		sb.append("    @Autowired\n");
		sb.append(
				"    public " + beanName + "RedisTemplate(RedisTemplate<String, " + beanName + "> redisTemplate) {\n");
		sb.append("        super(redisTemplate);\n");
		sb.append("    }\n\n");

		sb.append("}\n");
		writeS(new String(sb), "utf-8", javaFilePath + File.separator + beanName + "RedisTemplate.java");
	}

	// 4-1,根据读取的信息，创建实体service,{entity}Service.java\{entity}ServiceImpl.java
	public void createService() {
		// 创建包
		String javaFilePath = baseDir + File.separator + javaMakeCodeDir + File.separator + packagePath + ""
				+ File.separator + moduleName + File.separator + "service";
		createPackage(javaFilePath);
		// 代码
		StringBuffer sb = new StringBuffer();
		sb.append("package " + packageName + "." + moduleName + ".service;\n\n");
		sb.append("import java.util.*;\n");
		sb.append("import " + packageName + "." + moduleName + ".bean." + beanName + ";\n\n");

		sb.append("public interface " + beanName + "Service{\n\n");

		sb.append("    String add(" + beanName + " bean);\n\n");
		sb.append("    int del(String id);\n\n");
		sb.append("    int upt(" + beanName + " bean);\n\n");
		sb.append("    " + beanName + " get(String id);\n\n");
		sb.append("    List<" + beanName + "> list(int page, int pageSize,String sort," + beanName + " bean);\n\n");
		sb.append("    long count(" + beanName + " bean);\n\n");

		sb.append("}\n");
		writeS(new String(sb), "utf-8", javaFilePath + File.separator + beanName + "Service.java");
	}

	// 4-2,根据读取的信息，创建实体service,{entity}Service.java\{entity}ServiceImpl.java
	public void createServiceImpl() {
		// 创建包
		String javaFilePath = baseDir + File.separator + javaMakeCodeDir + File.separator + packagePath + ""
				+ File.separator + moduleName + File.separator + "service" + File.separator + "impl";
		createPackage(javaFilePath);
		// 代码
		StringBuffer sb = new StringBuffer();
		sb.append("package " + packageName + "." + moduleName + ".service.impl;\n\n");
		sb.append("import java.util.*;\n\n");

		sb.append("import com.aat.dizena.utils.Atools;\n");
		sb.append("import org.bson.types.ObjectId;\n");
		sb.append("import org.mongodb.morphia.Key;\n");
		sb.append("import org.mongodb.morphia.query.FindOptions;\n");
		sb.append("import org.mongodb.morphia.query.Query;\n");
		sb.append("import org.mongodb.morphia.query.UpdateOperations;\n");
		sb.append("import org.springframework.beans.factory.annotation.Autowired;\n");
		sb.append("import org.springframework.stereotype.Service;\n\n");

		sb.append("import " + packageName + "." + moduleName + ".bean." + beanName + ";\n");
		sb.append("import " + packageName + "." + moduleName + ".dao.mongo." + beanName + "Dao;\n");
		sb.append("import " + packageName + "." + moduleName + ".dao.redis." + beanName + "RedisTemplate;\n");
		sb.append("import " + packageName + "." + moduleName + ".service." + beanName + "Service;\n\n");

		sb.append("@Service\n");
		sb.append("public class " + beanName + "ServiceImpl  implements " + beanName + "Service {\n\n");

		sb.append("    @Autowired\n");
		sb.append("    private " + beanName + "Dao mongo;\n");
		// sb.append(" @Autowired\n");
		// sb.append(" private " + beanName + "RedisTemplate redis;\n\n");

		// add
		sb.append("    public String add(" + beanName + " bean){\n");
		sb.append("        Key<" + beanName + "> res = mongo.save(bean);\n");
		sb.append("        return res.getId().toString();\n");
		sb.append("    }\n\n");

		// del
		sb.append("    public int del(String id){\n");
		sb.append("        try{\n");
		sb.append("            ObjectId oid = new ObjectId(id);\n");
		sb.append("            return mongo.deleteById(oid).getN();\n");
		sb.append("        } catch (Exception e) {\n");
		sb.append("            return 0;\n");
		sb.append("        }\n");
		sb.append("    }\n\n");

		// upt
		sb.append("    public int upt(" + beanName + " bean){\n");
		sb.append("        try{\n");

		sb.append("            if(bean == null || bean.getId() == null) {\n");
		sb.append("                return 0;\n");
		sb.append("            }\n");

		sb.append("            Query<" + beanName + "> query = mongo.createQuery();\n");
		sb.append("        	   ObjectId oid = new ObjectId(bean.getId());\n");
		sb.append("            query.field(\"id\").equal(oid);\n");
		sb.append("            UpdateOperations<" + beanName + "> ops = mongo.createUpdateOperations();\n");
		for (TabField tabField : fields) {
			if (!"id".equals(tabField.getName())) {
				String fn1 = tabField.getName();
				String fn2 = fn1.substring(0, 1).toUpperCase() + fn1.substring(1, fn1.length());
				sb.append("            if(Atools.isNotEmpty(bean.get" + fn2 + "())){\n");
				sb.append("                ops.set(\"" + fn1 + "\",bean.get" + fn2 + "());\n");
				sb.append("            }\n");
			}
		}
		sb.append("            return mongo.update(query, ops).getUpdatedCount();\n");
		sb.append("        } catch (Exception e) {\n");
		sb.append("            return 0;\n");
		sb.append("        }\n");
		sb.append("    }\n\n");

		// query
		sb.append("    public " + beanName + " get(String id){\n");
		sb.append("        ObjectId oid = new ObjectId(id);\n");
		sb.append("        return mongo.get(oid);\n");
		sb.append("    }\n\n");

		sb.append("    public List<" + beanName + "> list(int page, int pageSize,String order," + beanName
				+ " bean){\n");
		sb.append("        Query<" + beanName + "> query = mongo.createQuery();\n");
		sb.append("        if (bean != null) {\n");
		for (TabField tabField : fields) {
			if (!"id".equals(tabField.getName())) {
				String fn1 = tabField.getName();
				String fn2 = fn1.substring(0, 1).toUpperCase() + fn1.substring(1, fn1.length());
				sb.append("            if(Atools.isNotEmpty(bean.get" + fn2 + "())){\n");
				sb.append("                query.field(\"" + fn1 + "\").equal(bean.get" + fn2 + "());\n");
				sb.append("            }\n");
			}
		}
		sb.append("        }\n");

		sb.append("        if (order != null) {\n");
		sb.append("            query.order(order);\n");
		sb.append("        }\n");

		sb.append("        FindOptions fo = new FindOptions();\n");
		sb.append("        fo.skip((page - 1) * pageSize);\n");
		sb.append("        fo.limit(pageSize);\n");
		sb.append("        return query.asList(fo);\n");
		sb.append("    }\n\n");

		sb.append("    public long count(" + beanName + " bean){\n");
		sb.append("        Query<" + beanName + "> query = mongo.createQuery();\n");
		sb.append("        if (bean != null) {\n");
		for (TabField tabField : fields) {
			if (!"id".equals(tabField.getName())) {
				String fn1 = tabField.getName();
				String fn2 = fn1.substring(0, 1).toUpperCase() + fn1.substring(1, fn1.length());
				sb.append("            if(Atools.isNotEmpty(bean.get" + fn2 + "())){\n");
				sb.append("                query.field(\"" + fn1 + "\").equal(bean.get" + fn2 + "());\n");
				sb.append("            }\n");
			}
		}
		sb.append("        }\n");
		sb.append("        return  mongo.count(query);\n");
		sb.append("    }\n\n");

		sb.append("}\n");
		writeS(new String(sb), "utf-8", javaFilePath + File.separator + beanName + "ServiceImpl.java");
	}

	// 5,根据读取的信息，创建实体controller
	public void createWebController() {
		// 创建包
		String javaFilePath = baseDir + javaMakeCodeDir + File.separator + packagePath + "" + File.separator
				+ moduleName + File.separator + "web" + File.separator + "controller";
		createPackage(javaFilePath);
		// 代码
		StringBuffer sb = new StringBuffer();
		sb.append("package " + packageName + "." + moduleName + ".web.controller;\n\n");

		sb.append("import java.util.*;\n\n");

		sb.append("import org.springframework.web.bind.annotation.*;\n");
		sb.append("import org.springframework.stereotype.Controller;\n");
		sb.append("import org.springframework.beans.factory.annotation.Autowired;\n");
		sb.append("import org.springframework.ui.Model;\n\n");

		sb.append("import com.aat.dizena.utils.Atools;\n");
		sb.append("import com.aat.dizena.base.web.controller.BaseController;\n\n");

		sb.append("import " + packageName + "." + moduleName + ".bean." + beanName + ";\n");
		sb.append("import " + packageName + "." + moduleName + ".service." + beanName + "Service;\n\n");

		sb.append("@Controller\n@RequestMapping(\"master/" + beanName.toLowerCase() + "\")\n");
		sb.append("public class Master" + beanName + "Controller  extends BaseController{\n\n");
		// base str
		sb.append("    @Autowired\n");
		sb.append("    private " + beanName + "Service " + beanName.toLowerCase() + "Service;\n\n");

		sb.append("    private String list = \"/master/" + beanName.toLowerCase() + "/list\";\n");
		sb.append("    private String edit = \"/master/" + beanName.toLowerCase() + "/edit\";\n");
		sb.append("    private String reList = \"redirect:\" + list;\n");
		sb.append("    private String addAction = \"master/" + beanName.toLowerCase() + "/add\";\n");
		sb.append("    private String uptAction = \"master/" + beanName.toLowerCase() + "/upt\";\n");
		sb.append("    private String delAction = \"master/" + beanName.toLowerCase() + "/del\";\n");

		sb.append("    private String StringKey = \" \";\n");
		sb.append("    private String addTitle = \"增加\"+StringKey;\n");
		sb.append("    private String uptTitle = \"修改\"+StringKey;\n");
		sb.append("    private String listHead = StringKey+\"管理\";\n\n");

		// list
		sb.append("    // list get\n");
		sb.append("    @RequestMapping(value = \"list\", method = RequestMethod.GET)\n");
		sb.append("    public String list(Model m) {\n");
		sb.append("        m.addAttribute(\"addAction\", addAction);\n");
		sb.append("        m.addAttribute(\"uptAction\", uptAction);\n");
		sb.append("        m.addAttribute(\"delAction\", delAction);\n");
		sb.append("        m.addAttribute(\"listHead\", listHead);\n");
		sb.append("        m.addAttribute(\"list\", list);\n");
		sb.append("        return list;\n");
		sb.append("    }\n\n");

		sb.append("    // list post\n");
		sb.append("    @RequestMapping(value = \"list\", method = RequestMethod.POST)\n");
		sb.append("    @ResponseBody\n");

		sb.append(
				"    public Map<String, Object> list(@RequestParam(value = \"s\", required = false, defaultValue = \"1\") int start,\n");
		sb.append("            @RequestParam(value = \"p\", required = false, defaultValue = \"10\") int perPage,\n");
		sb.append(
				"            @RequestParam(value = \"f\", required = false, defaultValue = \"ts\") String sortFiled,\n");
		sb.append(
				"            @RequestParam(value = \"dir\", required = false, defaultValue = \"DESC\") String direction,\n\n");
		sb.append("        @RequestParam(value = \"u\", required = false) String skey) {\n");

		sb.append("        " + beanName + " params = new " + beanName + "();\n\n");
		sb.append("        if(\"ASC\".equals(direction)) {\n\n");
		sb.append("        }else {\n");
		sb.append("            sortFiled=\"-\" + sortFiled;\n");
		sb.append("        }\n\n");

		sb.append("        //  返回参数\n");
		sb.append("        List<" + beanName + "> lists = " + beanName.toLowerCase()
				+ "Service.queryAll(params, start, perPage,sortFiled);\n");
		sb.append("        Long count = " + beanName.toLowerCase() + "Service.queryCount(params);\n");
		sb.append("        Long tmp = count / perPage;\n");
		sb.append("        Long pages = count % perPage == 0 ? tmp : tmp + 1;\n");
		sb.append("        Map<String, Object> json = new HashMap<String, Object>();\n");

		sb.append("        json.put(\"datas\", lists);// 数据\n");
		sb.append("        json.put(\"pages\", pages);// 总页\n");
		sb.append("        json.put(\"start\", start);// 页码\n");
		sb.append("        json.put(\"count\", count);// 总数\n");
		sb.append("        return json;\n");
		sb.append("    }\n\n");
		// list end

		// add
		sb.append("    // add get\n");
		sb.append("    @RequestMapping(value = \"add\", method = RequestMethod.GET)\n");
		sb.append("    public String add(Model model) {\n");
		sb.append("        model.addAttribute(\"action\", addAction);\n");
		sb.append("        model.addAttribute(\"title\", addTitle);\n");

		sb.append("        " + beanName + " m = new " + beanName + "();\n");
		sb.append("        m.setId(Atools.getOneKeyS());\n");
		sb.append("        model.addAttribute(\"m\", m);\n");
		sb.append("        return edit;\n");
		sb.append("    }\n\n");

		sb.append("    // add post\n");
		sb.append("    @RequestMapping(value = \"add\", method = RequestMethod.POST)\n");
		sb.append("    public String add(" + beanName + " m,Model model) {\n");
		sb.append("        model.addAttribute(\"action\", addAction);\n");
		sb.append("        model.addAttribute(\"title\", addTitle);\n");
		sb.append("        model.addAttribute(\"m\", m);\n");

		sb.append("        // 验证\n\n");
		sb.append("        //end \n");
		sb.append("        " + beanName.toLowerCase() + "Service.addBean(m);\n");
		sb.append("        return reList;\n");
		sb.append("    }\n\n");
		// add end

		// upt
		sb.append("    // upt get\n");
		sb.append("    @RequestMapping(value = \"upt/{id}\", method = RequestMethod.GET)\n");
		sb.append("    public String upt(@PathVariable(\"id\") String id,Model model) {\n");
		sb.append("        model.addAttribute(\"action\", uptAction);\n");
		sb.append("        model.addAttribute(\"title\", uptTitle);\n");

		sb.append("        " + beanName + " m = " + beanName.toLowerCase() + "Service.queryBean(id);\n");
		sb.append("        model.addAttribute(\"m\", m);\n");
		sb.append("        return edit;\n");
		sb.append("    }\n\n");

		sb.append("    // upt post\n");
		sb.append("    @RequestMapping(value = \"upt\", method = RequestMethod.POST)\n");
		sb.append("    public String upt(" + beanName + " m,Model model) {\n");
		sb.append("        model.addAttribute(\"action\", uptAction);\n");
		sb.append("        model.addAttribute(\"title\", uptTitle);\n");
		sb.append("        model.addAttribute(\"m\", m);\n");

		sb.append("        // 验证\n\n");
		sb.append("        //end \n");
		sb.append("        " + beanName.toLowerCase() + "Service.updateBean(m);\n");
		sb.append("        return reList;\n");
		sb.append("    }\n\n");
		// upt end

		// del
		sb.append("    // del\n");
		sb.append("    @RequestMapping(value = \"del/{id}\", method = RequestMethod.GET)\n");
		sb.append("    public String del(@PathVariable(\"id\") String id) {\n");
		sb.append("        " + beanName.toLowerCase() + "Service.deleteBean(id);\n");
		sb.append("        return reList;\n");
		sb.append("    }\n\n");
		// del end

		// ok
		sb.append("}\n");
		writeS(new String(sb), "utf-8", javaFilePath + File.separator + "Master" + beanName + "Controller.java");
	}

	// 100：取得XLSM 表格的值
	private static String getXlssVal(XSSFCell c) {
		try {
			if (c.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {
				return String.valueOf(c.getBooleanCellValue());
			} else if (c.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
				return String.valueOf(c.getNumericCellValue());
			} else {
				return String.valueOf(c.getStringCellValue());
			}
		} catch (Exception e) {
			return null;
		}
	}

	// 101：根据字段类型，得到java的属性类型；
	private String getFiledType(String str) {
		if (str.toLowerCase().contains("char")) {
			return "String";
		}
		if (str.toLowerCase().startsWith("int")) {
			return "Integer";
		}
		if (str.toLowerCase().contains("bigint")) {
			return "Long";
		}
		if (str.toLowerCase().contains("float") || str.toLowerCase().contains("double")
				|| str.toLowerCase().contains("num") || str.toLowerCase().contains("dec")) {
			return "Double";
		}
		if (str.toLowerCase().contains("date")) {
			return "Date";
		}
		if (str.toLowerCase().contains("bit")) {
			return "Boolean";
		}
		return "String";
	}

	// 102：创建文件包
	private void createPackage(String pathname) {
		File f = new File(pathname);
		if (!f.exists()) {
			f.mkdirs();
		}
	}

	// 103：判断不为空
	private boolean isNotEmpty(String str) {
		if (null == str) {
			return false;
		}
		if ("".equals(str) || str.length() == 0) {
			return false;
		}
		return true;

	}

	// 104：写出文件
	private void writeS(String str, String encoding, String sfile) {
		char[] cuf = str.toCharArray();// 将指定的字串序列化为byte[]
		try {
			int lastIndex = sfile.lastIndexOf(File.separator);
			String sfileDir = sfile.substring(0, lastIndex);
			File file = new File(sfileDir);
			if (!file.exists()) {
				file.mkdirs();
			}
			OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(sfile), encoding);
			out.write(cuf);
			out.flush();
			out.close();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	class TabField {
		private String desc;
		private String name;
		private String type;
		private String flag;

		public TabField(String desc, String name, String type, String flag) {
			super();
			this.desc = desc;
			this.name = name;
			this.type = type;
			this.flag = flag;
		}

		public String getDesc() {
			return desc;
		}

		public void setDesc(String desc) {
			this.desc = desc;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getType() {
			return getFiledType(type);
		}

		public void setType(String type) {
			this.type = type;
		}

		public String getFlag() {
			return flag;
		}

		public void setFlag(String flag) {
			this.flag = flag;
		}

	}
}
